package cn.youruan.demo.service.impl;


import cn.youruan.demo.common.CustomException;
import cn.youruan.demo.common.R;
import cn.youruan.demo.domain.Hobby;
import cn.youruan.demo.domain.Log;
import cn.youruan.demo.dto.HobbyDto;
import cn.youruan.demo.dto.UserDto;
import cn.youruan.demo.mapper.HobbyMapper;
import cn.youruan.demo.mapper.LogMapper;
import cn.youruan.demo.mapper.UserMapper;
import cn.youruan.demo.service.IHobbyService;
import cn.youruan.demo.service.ILogService;
import cn.youruan.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class HobbyServiceImpl extends ServiceImpl<HobbyMapper, Hobby> implements IHobbyService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IHobbyService hobbyService;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HobbyMapper hobbyMapper;

    @Override
    @Transactional
    public void addHobby(Hobby hobby, Long userId) {
        //1.先在查看当前用户是否已经20个爱好了
        Integer r = userService.selectHobbyCount(userId);
        if (r<20) {
            hobbyService.save(hobby);
            userService.addHobbyRelation(userId, hobby.getId());
        } else {
            //3.已经20个了抛出异常
            throw new CustomException("您已经达到爱好上限,无法添加...");
        }
    }

    @Override
    @Transactional
    public void removeHobbyById(Long userId,Long hobbyId) {
        //1.先在判断当前用户是否有爱好
        boolean r = userService.isHobby(userId);
        //2.如果没有提示无法删除
        if (!r) {
            throw new CustomException("您还未添加爱好,无法删除...");
        } else {
            //3.如果有才进行删除
           userService.deleteHobbyRelation(userId,hobbyId);
           hobbyService.removeById(hobbyId);
        }

    }



    @Override
    public List<HobbyDto> sortHotHobby() {
        return hobbyMapper.sortHotHobby();
    }
}
